<template>
    <div>
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span>添加公告</span>
            </div>

            <!-- 表单 -->
            <el-form size="mini" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
                class="demo-ruleForm">
                <el-form-item label="标题" prop="title">
                    <el-input class="elinput" v-model="ruleForm.title"></el-input>>
                </el-form-item>
                <el-form-item label="公告内容" prop="content">
                    <WangEditor @handleHtml="handleHtml"></WangEditor>
                </el-form-item>
                <el-form-item label="公告照片" prop="picture">
                    <el-upload class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/"
                        :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                    <el-radio-group v-model="ruleForm.state">
                        <el-radio :label="1">显示</el-radio>
                        <el-radio :label="2">隐藏</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-select v-model="ruleForm.type" placeholder="请选择">
                        <el-option label="区域一" value="shanghai"></el-option>
                        <el-option label="区域二" value="beijing"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
                    <el-button @click="resetForm('ruleForm')">取消</el-button>
                </el-form-item>
            </el-form>

        </el-card>
    </div>
</template>
<script>
import WangEditor from '@/components/WangEditor.vue';
export default {
    name: 'NewsAdd',
    components: { WangEditor },
    data() {
        return {
            imageUrl: '',
            resource: '',
            ruleForm: {
                state: 1,
            },
            rules: {
                title: [
                    { required: true, message: '请输入标题', trigger: 'blur' },
                ],
                region: [
                    { required: true, message: '请选择活动区域', trigger: 'change' }
                ],
            }
        }
    },
    created() {

    },
    methods: {
        handleHtml(html){
            this.ruleForm.content=html
        },
        // 文件
        handleAvatarSuccess(res, file) {
            this.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        // 表单
        submitForm(formName) {
            console.log('ruleform',this.ruleForm); 
            

            this.$refs[formName].validate((valid) => {
                if (valid) {
                    alert('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }

    }

}
</script>
<style lang="less" scoped>
/* 文件上传 */
/deep/ .avatar-uploader .el-upload {
    border: .1px dashed #d5d5d5;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    background-color: #fafafa;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #7e7e7e;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}

.avatar {
    width: 100px;
    height: 100px;
    display: block;
}

.text {
    font-size: 14px;
}

.item {
    margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
    display: table;
    content: "";
}

.clearfix:after {
    clear: both
}

.box-card {
    margin: auto;
    width: 90%;
}

.elinput {
    width: 20%;
}
</style>